package servlet.admin;

import dao.FeeTypeDao;
import entity.Admin;
import entity.FeeType;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/admin/feeType/*")
public class FeeTypeServlet extends HttpServlet {
    private FeeTypeDao feeTypeDao = new FeeTypeDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getPathInfo();

        // 检查管理员登录状态
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin == null) {
            response.sendRedirect(request.getContextPath() + "/adminLogin.jsp");
            return;
        }

        try {
            if ("/delete".equals(action)) {
                deleteFeeType(request, response);
            } else {
                response.sendRedirect(request.getContextPath() + "/admin/feeTypeManage.jsp");
            }
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
            request.getRequestDispatcher("/admin/feeTypeManage.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getPathInfo();

        // 检查管理员登录状态
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin == null) {
            response.sendRedirect(request.getContextPath() + "/adminLogin.jsp");
            return;
        }

        try {
            switch (action) {
                case "/add":
                    addFeeType(request, response);
                    break;
                case "/update":
                    updateFeeType(request, response);
                    break;
                case "/delete":
                    deleteFeeType(request, response);
                    break;
                default:
                    response.sendRedirect(request.getContextPath() + "/admin/feeTypeManage.jsp");
            }
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
            request.getRequestDispatcher("/admin/feeTypeManage.jsp").forward(request, response);
        }
    }

    private void addFeeType(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");

        FeeType feeType = new FeeType();
        feeType.setName(request.getParameter("name"));
        feeType.setPrice(new BigDecimal(request.getParameter("price")));
        feeType.setUnit(request.getParameter("unit"));
        feeType.setDescription(request.getParameter("description"));
        feeType.setStatus(1);

        feeTypeDao.addFeeType(feeType);
        response.sendRedirect(request.getContextPath() + "/admin/feeTypeManage.jsp");
    }

    private void updateFeeType(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        FeeType feeType = new FeeType();
        feeType.setId(Integer.parseInt(request.getParameter("id")));
        feeType.setName(request.getParameter("name"));
        feeType.setPrice(new BigDecimal(request.getParameter("price")));
        feeType.setUnit(request.getParameter("unit"));
        feeType.setDescription(request.getParameter("description"));
        feeType.setStatus(Integer.parseInt(request.getParameter("status")));

        feeTypeDao.updateFeeType(feeType);
        response.sendRedirect(request.getContextPath() + "/admin/feeTypeManage.jsp");
    }

    private void deleteFeeType(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        feeTypeDao.deleteFeeType(id);
        response.sendRedirect(request.getContextPath() + "/admin/feeTypeManage.jsp");
    }
}